package com.yanfan.information.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {

    // MD5加密
    public static String md5(String input) {
        try {
            // 创建MD5摘要实例
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算摘要
            byte[] messageDigest = md.digest(input.getBytes());
            // 将字节数组转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    // 测试
    public static void main(String[] args) {
        String input = "admin12345";
        String md5Hash = md5(input);
        System.out.println("MD5 Hash: " + md5Hash);
    }
}
